package com.wk.dao.impl;

import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.mongodb.DBObject;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSFile;
import com.wk.dao.IFileUploadDAO;
import com.wk.dao.IGridFsDAO;
import com.wk.dao.IMongodbDAO;
import com.wk.util.ConstantUtil;

/**
 * 
 * TODO 在此写上类的相关说明.<br>
 * @author User <br>
 * @version 1.0.0 2014年3月6日<br>
 * @see 
 * @since
 */
@Repository
public class FileUploadDao implements IFileUploadDAO {

	@Autowired
	private IMongodbDAO dao; 
	
	@Autowired
	private IGridFsDAO gridFsDao;
	
	
	@Override
	public String saveFile(InputStream content, String filename,String contentType){
		return gridFsDao.storeFile(content, filename,contentType);
	}
	
	
	
	

	@Override
	public Map<String,Object> queryFile(String fileId){
		Map<String,Object> attachMap = new HashMap<String,Object>();
		try {
			GridFSDBFile fsFile = gridFsDao.getFile(fileId) ;
			//判断附件对象是否为Null
			if(fsFile != null){
			    BufferedImage image = ImageIO.read(fsFile.getInputStream());
				//因为上传图片的时候做了大、中、小3种类型图片的压缩处理，图片名后面带着1280、600、300的宽度值，返回前台显示的时候要去掉后面的宽度值
				String fileType = fsFile.getContentType();
				String fileName = fsFile.getFilename();
				if(fileType.toLowerCase().equals("bmp") || fileType.toLowerCase().equals("jpeg") 
						|| fileType.toLowerCase().equals("jpg") || fileType.toLowerCase().equals("png")){
					String largePic = String.valueOf(ConstantUtil.PIC_LARGE_SIZE);
					String middlePic = String.valueOf(ConstantUtil.PIC_MIDDLE_SIZE);
					String smallPic = String.valueOf(ConstantUtil.PIC_SMALL_SIZE);
					int index = fileName.lastIndexOf(".");
					String fileNameNoExe = fileName;
					if(index!=-1){
						fileNameNoExe = fileName.substring(0,index);
					}
					if(fileNameNoExe.endsWith(largePic)){
						fileName = fileNameNoExe.substring(0,fileNameNoExe.lastIndexOf(largePic));
					}else if(fileNameNoExe.endsWith(middlePic)){
						fileName = fileNameNoExe.substring(0,fileNameNoExe.lastIndexOf(middlePic));
					}else if(fileNameNoExe.endsWith(smallPic)){
						fileName = fileNameNoExe.substring(0,fileNameNoExe.lastIndexOf(smallPic));
					}
				}
				
				if(fileName.contains(".")){
					attachMap.put("fileName", fileName);	
				}else{
					attachMap.put("fileName", fileName +"." +fsFile.getContentType());	
				}
				
				attachMap.put("fileType", fsFile.getContentType());
				attachMap.put("md5", fsFile.getMD5());
				attachMap.put("uploadDate", fsFile.getUploadDate());
				attachMap.put("fileLength", fsFile.getLength());
				if(image !=null){
				    attachMap.put("picLong", image.getHeight());
	                attachMap.put("picWidth", image.getWidth());
				}
				
				DBObject obj = fsFile.getMetaData();
				if(obj!=null){
					String pdfUrl = obj.get("pdfViewUrl")!=null?obj.get("pdfViewUrl").toString():"";
					if(StringUtils.isNotEmpty(pdfUrl)){
						attachMap.put("pdfViewUrl", pdfUrl);
					}
					Object state = obj.get("convertState");
					Object userId = obj.get("userId");
					Object radioLength = obj.get("radioLength");
					if(state!=null){
						attachMap.put("convertState", state);
					}
					if(userId!=null){
						attachMap.put("userId", userId);
					}
					if(radioLength !=null){
						attachMap.put("radioLength", radioLength);
					}
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return attachMap;
	}

	@Override
	public void removeAttachs(String fileId){
		 
		gridFsDao.deleteFile(fileId);
	}
	
	@Override
	public InputStream queryFileInputStream(String fileId) {
		 
		return gridFsDao.getInputStream(fileId);
	}

	@Override
	public String saveFile(InputStream content, String filename,
			String contentType, Map<String, Object> object) {
		return this.gridFsDao.storeFile(content, filename, contentType, object);
	}

	@Override
	public void updateFileProperty(String attachsId, long radioLength) {
		GridFSDBFile fsFile = gridFsDao.getFile(attachsId) ;
		DBObject obj = fsFile.getMetaData();
		if(obj != null){
			obj.put("radioLength", radioLength);
			fsFile.setMetaData(obj);
			fsFile.save();	
		}
	}

	
	@Override
	public GridFSFile getAttachsByGridsFile(InputStream content,
			String filename, String contentType) {
		 GridFSFile file = gridFsDao.storeGridFile(content, filename, contentType);
		 return file;
	}

	@Override
	public Map<String, Object> queryDownFile(String fileId) {
		Map<String,Object> fileMap = new HashMap<String,Object>();
		GridFSDBFile file = gridFsDao.getFile(fileId);
		if(file != null){
			fileMap.put("fileName", file.getFilename());
			fileMap.put("fileLength", file.getLength());
			fileMap.put("inputStream", file.getInputStream());
		}
		
		return fileMap;
	}
}
